//
//  YTCollectAddrCell.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/12.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTCollectAddrCell.h"
@interface YTCollectAddrCell ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addrLabel;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
@property (weak, nonatomic) IBOutlet UIButton *editButton;
@property (weak, nonatomic) IBOutlet UIButton *defaultAddrButton;

@end
@implementation YTCollectAddrCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
    [self corRadusBorderWithView:self.deleteButton];
    [self corRadusBorderWithView:self.editButton];
}
- (IBAction)defaultAddrBtnClick:(UIButton *)sender
{
    if (sender.selected) return;
    
    
    sender.enabled = false;
    [YTNetworkManager.shareManager uploadDefaultAddrWithID:self.model.ID
                                                completion:^(BOOL isSucc, NSString *error)
    {
        sender.enabled = YES;
        if (!isSucc) {
            [self showErrorWithTipStr:error];
        }
        
        NSAssert(self.selectDefaultAddr != nil, @"selectDefaultAddr 不能为nil");
        self.selectDefaultAddr(sender);
    }];
  
}

#pragma mark: - 编辑按钮 监听
- (IBAction)editeButtonClick
{
    NSAssert(self.editeBtnClickBlock != nil, @"editeBtnClickBlock 不能为nil");
    self.editeBtnClickBlock(self.model.ID);
}

- (void)setModel:(YTCollectAddrModel *)model
{
    _model = model;
    
    self.nameLabel.text = model.collect_name;
    self.addrLabel.text = model.addr;
    self.defaultAddrButton.selected = model.is_default.boolValue;
    
    if (model.is_default.boolValue) {
        NSAssert(self.selectDefaultAddr != nil, @"selectDefaultAddr 不能为nil");
        self.selectDefaultAddr(self.defaultAddrButton);
    }
}

#pragma mark: - 删除按钮 监听
- (IBAction)deleteBtnClick:(UIButton *)sender
{
    
    sender.enabled = false;
    [YTNetworkManager.shareManager requestDeleteAddrWithIsWallet:false
                                                              ID:self.model.ID
                                                      completion:^(BOOL isSucc, NSString *error)
    {
        NSAssert(self.deleteAddrBtnClickBlock != nil, @"deleteAddrBtnClickBlock 不能为nil");
        sender.enabled = YES;
        if (!isSucc) {
            self.deleteAddrBtnClickBlock(false, error);
            return ;
        }
        
        self.deleteAddrBtnClickBlock(YES, nil);
    }];
 
    
}

- (void)corRadusBorderWithView:(UIView *)view
{
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;
    view.layer.borderWidth = 1;
    view.layer.borderColor = [UIColor.blackColor CGColor];
}
@end
